﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;

namespace myChannel9
{
    public partial class VideoInfo : PhoneApplicationPage
    {
        List<VideoObject> channel9List = new List<VideoObject>();
        VideoObject video = new VideoObject();

        public VideoInfo()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsolatedStorageSettings.ApplicationSettings.Contains("Video"))
            {
                video = IsolatedStorageSettings.ApplicationSettings["Video"] as VideoObject;

                if (IsolatedStorageSettings.ApplicationSettings.Contains("channel9List"))
                {
                    channel9List = IsolatedStorageSettings.ApplicationSettings["channel9List"] as List<VideoObject>;

                    var tempList =
                    from channel9 in channel9List
                    orderby channel9.PostedDate descending
                    where channel9.Url.ToLower().Contains(video.Url.ToLower())
                    select channel9;

                    List<VideoObject> list = tempList.ToList();

                    if (list.Count() > 0)
                    {
                        video = list[0] as VideoObject;
                    }
                }

                lblDate.Text = video.getPostedDetails;
                lblLenght.Text = video.getLenght;
                lblLenght.Visibility = Visibility.Visible;
                lblHeading.Text = video.Heading;
                lblDescription.Text = video.Description;

                Uri uri = new Uri(video.getImage, UriKind.Absolute);
                ImageSource imgSource = new BitmapImage(uri);
                img.Source = imgSource;

                showCorrectFavoriteIcon();
            }
        }

        private void showCorrectFavoriteIcon()
        {
            string imageName1 = "", imageName2 = "";

            if (video.Favorite)
            {
                imageName1 = "Images/imgStar4.png";
            }
            else
            {
                imageName1 = "Images/imgStar.png";
            }

            Uri uri = new Uri(imageName1, UriKind.Relative);
            ImageSource imgSource = new BitmapImage(uri);
            btnFavorite.Source = imgSource;

            uri = new Uri(imageName2, UriKind.Relative);
            imgSource = new BitmapImage(uri);
        }

        private void btnFavorite_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (channel9List != null)
            {
                if (video.Favorite)
                {
                    channel9List.Remove(video);
                    video.Favorite = false;
                    channel9List.Add(video);
                }
                else
                {
                    channel9List.Remove(video);
                    video.Favorite = true;
                    channel9List.Add(video);
                }

                showCorrectFavoriteIcon();

                IsolatedStorageSettings.ApplicationSettings["channel9List"] = channel9List;
            }
        }

        private void btnPlay_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Microsoft.Phone.Tasks.MediaPlayerLauncher mediaPlayerLauncher = new Microsoft.Phone.Tasks.MediaPlayerLauncher();
            mediaPlayerLauncher.Media = new Uri(video.getVideo);
            mediaPlayerLauncher.Show();
        }
    }
}